namespace ETestIT.Core.Storage
{
    using System;
    using System.Collections;
    using System.Web;

    public class SessionStorage : Store, ISessionStorage
    {
        // Methods
        protected override Hashtable GetInternalHashtable()
        {
            if(!Store.IsWebApplication || (HttpContext.Current.Session == null))
                throw new InvalidOperationException("An ASP.Net session must be available when using Session storage. No ASP.Net session was found in the current context.");

            Hashtable internalStorage = HttpContext.Current.Session[typeof(SessionStorage).FullName] as Hashtable;
            if (internalStorage == null)
            {
                lock (Store.SessionStorageLock)
                {
                    internalStorage = HttpContext.Current.Session[typeof(SessionStorage).FullName] as Hashtable;
                    if (internalStorage == null)
                    {
                        HttpContext.Current.Session[typeof(SessionStorage).FullName] = internalStorage = new Hashtable();
                    }
                }
            }
            return internalStorage;
        }

        // Properties
        protected override object LockInstance
        {
            get
            {
                return Store.SessionStorageLock;
            }
        }

        protected override bool UseLocking
        {
            get
            {
                return true;
            }
        }
    }
}